package com.ice.netty.web;

import com.ice.framework.common.annotation.AutoResult;
import com.ice.netty.protobuf.msg.ProtoMsg;
import com.ice.netty.protobuf.server.ProtoServer;
import com.ice.netty.web.dto.ScanCodeOpenReq;
import com.ice.remoting.data.RemotingCommand;
import com.ice.remoting.exception.RemotingConnectException;
import com.ice.remoting.exception.RemotingSendRequestException;
import com.ice.remoting.exception.RemotingTimeoutException;
import com.ice.remoting.utils.NetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@AutoResult
@Slf4j
@RestController
public class ProtobufController {
    @Autowired
    private ProtoServer protoServer;

    @GetMapping("/protoScanCodeOpen")
    public void scanCodeOpen( )
            throws RemotingSendRequestException, RemotingConnectException, RemotingTimeoutException, InterruptedException {
        ScanCodeOpenReq scanCodeOpenReq = new ScanCodeOpenReq();
        scanCodeOpenReq.setDeviceNo("52125658");
        RemotingCommand command = new RemotingCommand();
        ProtoMsg.Message.Builder builder = ProtoMsg.Message.newBuilder();
        ProtoMsg.ScanCodeReq.Builder scanCodeReq = ProtoMsg.ScanCodeReq.newBuilder();
        scanCodeReq.setOpenFlag(1);
        builder.setType(ProtoMsg.HeadType.SCANCODEREQ);
        builder.setScanCodeReq(scanCodeReq);
        command.setOpaque(RemotingCommand.createNewRequestId());
        command.setBody(builder.build().toByteArray());
        RemotingCommand remotingCommand = protoServer.invokeSync(scanCodeOpenReq.getDeviceNo(), command);
        log.info("接受到数据：" + NetUtil.byte2HexStrSpace(remotingCommand.getBody()));
    }
}
